import java.util.Arrays;

import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOf;

public class TestDemo {
    public static void main(String[] args) {
        int[][] arr=new int[3][];
        //初始化不规则二维数组
        arr[0]=new int[2];
        arr[1]=new int[6];
        arr[2]=new int[3];
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
    //打印二维数组
    public static void main13(String[] args) {
        int[][] array={{1,2,3},{4,5,6}};
        //for循环打印
        System.out.println("===========for循环打印=========");
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array[i].length;j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        //for-each打印
        System.out.println("===========for-each打印=========");
        for (int[] arr:array) {
            for (int x:arr) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        //库函数打印 Arrays.deepToString（）
        System.out.println("===========库函数打印=========");
        System.out.println(Arrays.deepToString(array));
    }
    public static void main12(String[] args) {
        int[] arr={1,2,3,4};
        System.out.println(Arrays.toString(arr));
        Arrays.fill(arr,99);//整体填充
        System.out.println("整体填充："+Arrays.toString(arr));
        Arrays.fill(arr,0,2,100);//局部填充
        System.out.println("局部填充："+Arrays.toString(arr));
    }
    public static void main11(String[] args) {
        int[] arr1={1,2,3,4};
        int[] arr2={1,2,3,4};
        System.out.println(Arrays.equals(arr1, arr2));//true
    }
    //冒泡排序
    public static void bubbleSort(int[] arr){
        for(int i=0;i<arr.length-1;i++){
            boolean flg=false;
            for(int j=0;j<arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
                flg=true;
            }
            if(!flg){
                break;
            }
        }
    }
    public static void main9(String[] args) {
        int[] arr={1,3,2,6,4,7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    //数组中偶数放在前面，奇数放在后面（忽略大小顺序）
    public static void sort(int[] arr){
        int i=0;
        int j=arr.length-1;
        while(i<j){
            //这里写 i<j 是为了防止 i 越界
            while(i<j && arr[i]%2==0){
                i++;
            }
            //这里写 i<j 是为了防止 j 越界
            while(i<j && arr[j]%2==1){
                j--;
            }
            int temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
        }
    }
    public static void main10(String[] args) {
        int[] arr={2,3,2,5,4};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    //判断数组是不是有序递增的
    public static boolean isSorted(int[] arr){
        if(arr==null) return false;
        for(int i=0;i<arr.length-1;i++){
            if(arr[i+1]<=arr[i]){
                return false;
            }
        }
        return true;
    }
    public static void main8(String[] args) {
        int[] arr={1,2,3,4};
        System.out.println(isSorted(arr));
    }
    public static void main7(String[] args) {
        int[] array={1,2,3,4};
        int[] copy=array.clone();
        System.out.println(Arrays.toString(copy));
    }
    public static void main6(String[] args) {
        //arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);
        //src:原始数组  srcPos:原始数组的下标
        //dest:目的地数组  destPos:目的地数组的下标 length:需要拷贝的长度
        int[] array={1,2,3,4};
        int[] copy=new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }
    public static void main5(String[] args) {
        int[] array={1,2,3,4};
        //copyOf(int[] original, int newLength)
        //original:原始的数组  newLength:新数组的长度(可以自己定义)
        int[] array2= copyOf(array,array.length);
        System.out.println(Arrays.toString(array2));

        //拷贝某个数组的范围
        //copyOfRange(int[] original, int from, int to)
        int[] array3=Arrays.copyOfRange(array,2,3);
        System.out.println(Arrays.toString(array3));
    }
    public static int binarySearch(int[] array,int key){
        int l=0;
        int r=array.length-1;

        while (l <= r){
            int mid = (l+r)/2;
            if(array[mid] > key){
                r=mid-1;
            }else if(array[mid] < key){
                l=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static void main4(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8};
        int key=10;
        System.out.println(binarySearch(arr,key));
    }
    //找出数组中的最大值
    public static int max(int[] arr){
        int max=arr[0];
        for(int i=1;i<arr.length;i++){
            max=max>arr[i]?max:arr[i];
        }
        return max;
    }
    public static void main3(String[] args) {
        int[] arr={1,6,3,8,3,5,6,7};
        System.out.println(max(arr));
    }
    //实现一个toString
    public static String myToString(int[] array){
        if(array==null) return "null";
        String str="[";
        for(int i=0;i<array.length;i++){
            str+=array[i];
            if(i<array.length-1){
                str+=",";
            }
        }
        str+="]";
        return str;
    }

    public static void main2(String[] args) {
        int[] arr={1,22,3};
        System.out.println(myToString(arr));
    }
    public static double avgArr(int[] arr){
        int len=arr.length;
        double sum=0;
        for(int i=0;i<len;i++){
            sum+=arr[i];
        }
        return sum/len;
    }
    public static void main1(String[] args) {
        int[] arr={1,2,3,4,6};
        System.out.println(avgArr(arr));
    }
}
